package com.luxoft.scoremanager.server.model.entity;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table
public class ScorePackage {
    private static final long serialVersionUID = -5170875020617735664L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen")
    @SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "SP_SEQ")
    @Column(name = "score_Package_ID")
    private Integer id;

    @Column(name = "stage")
    private String stage;

    @OneToMany(mappedBy = "scorePackage", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SUBSELECT)
    @OrderBy("id")
    private List<ComponentValue> componentsValues = new ArrayList<ComponentValue>();

    @Override
    public String toString() {
        return "ScorePackage{" +
                "id=" + id +
                ", stage='" + stage +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ScorePackage that = (ScorePackage) o;

        if (!id.equals(that.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    public Integer getId() {

        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getStage() {
        return stage;
    }

    public void setStage(String stage) {
        this.stage = stage;
    }

    public void addComponentValues(ComponentValue componentsValues) {
        this.componentsValues.add(componentsValues);
    }

    public void deleteComponentValues(ComponentValue componentsValues) {
        if (this.componentsValues.contains(componentsValues)) {
            this.componentsValues.remove(componentsValues);
        }
    }

    public List<ComponentValue> getComponentsValues() {
        return componentsValues;
    }

    public void setComponentValues(List<ComponentValue> componentsValues) {
        this.componentsValues = componentsValues;
    }

    public ScorePackage(String stage) {

        this.stage = stage;
    }

    @Deprecated
    public ScorePackage(Integer id, String stage) {
        this.setId(id);
        this.stage = stage;
    }

    public ScorePackage() {

    }
}
